package com.symbio.ishenapi.patient.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.bean.QueryModel;
import com.symbio.ishenapi.patient.bean.PatientBloodPressure;
import com.symbio.ishenapi.patient.bean.PatientBloodPressureAttr;
import com.symbio.ishenapi.patient.bean.PatientBloodPressureResponse;
import io.jboot.db.annotation.Table;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientBloodPressure;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generated by Jboot.
 */
@Table(tableName = "tm_patient_blood_pressure", primaryKey = "id")
public class TmPatientBloodPressure extends BaseTmPatientBloodPressure<TmPatientBloodPressure> {

    public static TmPatientBloodPressure dao = new TmPatientBloodPressure();

    /**
     * @return
     * @Author GuoShunYou
     * @Description 通过userId、runtime（日期）查询血压记录
     * @date 下午 2:06   2018/10/23 0023
     */
    public PatientBloodPressure findTmPatientBloodPressure(PatientBloodPressure baseTmPatientBloodPressure) {     //查询血压
        String sql = ("SELECT p.userId userid,p.runtime runtime,pa.pressureType pressuretype,pa.pressureValue val,pa.timequantum timequantum from tm_patient_blood_pressure p,tm_patient_blood_pressure_attr pa where p.id=pa.pressureId and p.status=1 and p.runtime='" + baseTmPatientBloodPressure.getRuntime() + "' and p.`userId`=" + baseTmPatientBloodPressure.getUserId() + "");
        List<Record> list = Db.find(sql);
        PatientBloodPressure p = new PatientBloodPressure();
        if(list==null){
            return p;
        }

        List<PatientBloodPressureAttr> listpattr = new ArrayList<>();
        int userId = 0;
        String runtime = "";
        int coun = 0;
        PatientBloodPressureAttr pattr = new PatientBloodPressureAttr();
        for (int i = 0; i < list.size(); i++) {
            coun++;
            userId = Integer.valueOf(list.get(i).getStr("userid"));
            runtime = list.get(i).getStr("runtime");
            if ("200000025".equals(list.get(i).getStr("pressuretype"))) {
                pattr.setHeight(list.get(i).getStr("val"));
            } else if ("200000026".equals(list.get(i).getStr("pressuretype"))) {
                pattr.setLow(list.get(i).getStr("val"));
            }
            pattr.setTime(list.get(i).getStr("timequantum"));
            if (coun % 2 == 0) {
                System.out.println(coun);
                System.out.println(pattr.getHeight() + "  " + pattr.getLow() + "  " + pattr.getTime());
                listpattr.add(pattr);
                pattr = new PatientBloodPressureAttr();
            }
        }
        String usid = String.valueOf(userId);
        BigInteger bigint = new BigInteger(usid);
        p.setUserId(bigint);
        p.setList(listpattr);
        p.setRuntime(runtime);
        return p;
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通過userId查詢所有血壓記錄
     * @date 下午 5:38   2018/10/23
     * @return java.util.List<com.symbio.ishenapi.patient.bean.PatientBloodPressureResponse>
     */
    public List<PatientBloodPressureResponse> findAll(TmPatientBloodPressure tmp){
        String sql="SELECT pa.id ,p.userId userId,p.runtime runtime,pa.pressureType pressureType,pa.pressureValue pressureValue,pa.timequantum timequantum from tm_patient_blood_pressure p,tm_patient_blood_pressure_attr pa where p.id=pa.pressureId and p.userId="+tmp.getUserId()+" and p.`status`=1 order by p.runtime";
        List<Record> list=Db.find(sql);
        List<PatientBloodPressureResponse> prelist=new ArrayList<PatientBloodPressureResponse>();
        for(int i=0;i<list.size();i++){
            PatientBloodPressureResponse pa=new PatientBloodPressureResponse();
            String userid=list.get(i).getStr("userId");
            pa.setUserId(Integer.valueOf(userid));
            String runtime=list.get(i).getStr("runtime");
            pa.setRuntime(runtime);
            String pressureType=list.get(i).getStr("pressureType");
            pa.setPressureType(pressureType);
            String pressureValue=list.get(i).getStr("pressureValue");
            pa.setPressureValue(pressureValue);
            String timequantum=list.get(i).getStr("timequantum");
            pa.setTimequantum(timequantum);
            prelist.add(pa);
        }
        return prelist;

    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根據userID、runtime(日期)查詢當天血壓最高值
     * @date 下午 6:24   2018/10/23 0023
     * @return java.lang.String
     */
    public String getHeight(Object runtime,Object userid){
        String sql="SELECT pa.id,p.userId,p.runtime,pa.pressureType,pa.pressureValue val,pa.timequantum from tm_patient_blood_pressure p,tm_patient_blood_pressure_attr pa where p.id=pa.pressureId and p.runtime='"+runtime+"' and p.userId="+userid+" and p.`status`=1 ORDER BY pa.pressureValue desc";
        Record re=Db.findFirst(sql);
        String ss=re.getStr("val");
        return ss;
    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根據userID、runtime(日期)查詢當天血壓最低值
     * @date 下午 6:24   2018/10/23 0023
     * @return java.lang.String
     */
    public String getLow(Object runtime,Object userid){
        String sql="SELECT pa.id,p.userId,p.runtime,pa.pressureType,pa.pressureValue val,pa.timequantum from tm_patient_blood_pressure p,tm_patient_blood_pressure_attr pa where p.id=pa.pressureId and p.runtime='"+runtime+"' and p.userId="+userid+" and p.`status`=1 ORDER BY pa.pressureValue";
        Record re=Db.findFirst(sql);
        String ss=re.getStr("val");
        return ss;
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根據userID查詢日期分組
     * @date 下午 8:55   2018/10/23 0023
     * @return java.util.List<com.symbio.ishenapi.patient.model.TmPatientBloodPressure>
     */
    public List<String> getRuntime(TmPatientBloodPressure tmp){
        String sql="SELECT runtime runtimes FROM tm_patient_blood_pressure  WHERE userId="+tmp.getUserId()+" and status=1  GROUP BY runtime ORDER BY runtime ";
        List<TmPatientBloodPressure> list=dao.find(sql);
        List<String> lis=new ArrayList<String>();
        for(int i=0;i<list.size();i++){
            String time=list.get(i).getStr("runtimes");
            lis.add(time);
        }
        return lis;
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通过血压主表id获取所关联的附表id集合
     * @date 下午 3:16   2018/10/24 0024
     * @return java.util.List<java.lang.String>
     */
    public List<String> getIdList(Object pressureid){
        String sql="select id ids from tm_patient_blood_pressure_attr where pressureId="+pressureid+"";
        List<TmPatientBloodPressure> lisst=dao.find(sql);
        List<String> li=new ArrayList<String>();
        for(int i =0;i<lisst.size();i++){
            String id=lisst.get(i).getStr("ids");
            li.add(id);
        }
        return li;

    }

    public List<TmPatientBloodPressure> findOnePressure(BaseTmPatientBloodPressure baseTmPatientBloodPressure){     //查询血压一条数据
        String sql  = ("select * from tm_patient_blood_pressure tpu where tpu.userId = ?  and tpu.status = '1' order by createTime desc LIMIT 1 ");
        List<TmPatientBloodPressure> find = dao.find(sql,baseTmPatientBloodPressure.getUserId());
        return find;
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userid、runtime查询是否有该数据
     * @date 下午 5:27   2018/10/28
     * @return int
     */
    public int selectCount(PatientBloodPressure tmp){
        String sql="select COUNT(1) cou from tm_patient_blood_pressure where userId="+tmp.getUserId()+" and runtime='"+tmp.getRuntime()+"'";
        TmPatientBloodPressure tmpp=dao.findFirst(sql);
        String nm=tmpp.getStr("cou");
        int num=Integer.valueOf(nm);
        return num;

    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通过userid、runtime查询对应数据主id
     * @date 下午 5:34   2018/10/28
     * @return int
     */
    public BigInteger selectId(PatientBloodPressure tmp){
        String sql="select id from tm_patient_blood_pressure where userId="+tmp.getUserId()+" and runtime='"+tmp.getRuntime()+"'";
        TmPatientBloodPressure tmm=dao.findFirst(sql);
        return tmm.getId();
    }



    public Map findPatientBloodPressure(BigInteger userId, QueryModel queryModel){     //查询体重
        Map<String,Object> map = new HashMap<>();
        String sqlCount = "select count(id) from tm_patient_blood_sugar";
        String sql = "select * from tm_patient_blood_sugar tpbs join tm_patient_blood_sugar_attr tpbsa on tpbsa.sugarId=tpbs.id where userId = "+userId+" and tpbs.`status` = 1 ORDER BY tpbs.runtime DESC LIMIT "+queryModel.getPage()+","+queryModel.getLimit()+"";
        int count = Db.queryInt(sqlCount);
        List<Record> data = Db.find(sql);
        map.put("count",count);
        map.put("data",data);
        return map;
    }
}
